<?php

namespace app\admin\controller\house;

use app\admin\model\User;
use app\api\model\house\HouseFacilities;
use app\api\model\house\HouseFacilitiesCategory;
use app\api\model\house\HouseManage;
use app\api\model\house\HouseManageCategory;
use app\api\model\house\HouseMedia;
use app\api\model\house\HouseMediaCategory;
use app\api\model\house\HouseParkComplement;
use app\api\model\house\HouseParkMedia;
use app\api\model\house\HouseType;
use app\common\controller\Backend;

/**
 * 房屋
 *
 * @icon fa fa-circle-o
 */
class House extends Backend
{

    /**
     * House模型对象
     * @var \app\admin\model\house\House
     */
    protected $model = null;
    protected $HouseBedInfoModel = null;
    protected $HouseFacilitiesModel = null;
    protected $HouseComplementPriceModel = null;
    protected $HouseParkModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\house\House;
        $this->HouseBedInfoModel = new \app\admin\model\house\HouseBedInfo();
        $this->HouseFacilitiesModel = new \app\admin\model\house\HouseFacilities();
        $this->HouseComplementPriceModel = new \app\admin\model\house\HouseComplementPrice();
        $this->HouseParkModel = new \app\admin\model\house\HousePark();
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['house_id','user_id','house_title','address','province','city','house_number','createtime','status',
                    'house_attr','house_level','is_top','is_recommend','house_code']);
                $row->visible(['user']);
				$row->getRelation('user')->visible(['nickname','avatar']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    //详情
    public function detail($ids = null)
    {
        $row = $this->model->get($ids);
        if ($row->house_property_cert_img){
            $row->house_property_cert_img = explode(',',$row->house_property_cert_img);
        }  else {
            $row->house_property_cert_img = [];
        }

        if ($row->purchase_contract_img){
            $row->purchase_contract_img = explode(',',$row->purchase_contract_img);
        } else {
            $row->purchase_contract_img = [];
        }

        if ($row->evaluation_report_img){
            $row->evaluation_report_img = explode(',',$row->evaluation_report_img);
        } else {
            $row->evaluation_report_img = [];
        }

        if ($row->renovation_contract_img){
            $row->renovation_contract_img = explode(',',$row->renovation_contract_img);
        } else {
            $row->renovation_contract_img = [];
        }

        if ($row->insurance_contract_img){
            $row->insurance_contract_img = explode(',',$row->insurance_contract_img);
        } else {
            $row->insurance_contract_img = [];
        }

        if ($row->receive_time){
            $row->receive_time = json_decode($row->receive_time,true);
        } else {
            $row->receive_time = [];
        }

        if ($row->stay_discount){
            $row->stay_discount = json_decode($row->stay_discount,true);
        } else {
            $row->stay_discount = [];
        }


        if ($row->group_weekday_price){
            $row->group_weekday_price = json_decode($row->group_weekday_price,true);
        } else {
            $row->group_weekday_price = [];
        }

        if ($row->group_weekend_price){
            $row->group_weekend_price = json_decode($row->group_weekend_price,true);
        } else {
            $row->group_weekend_price = [];
        }

        if ($row->group_highday_price){
            $row->group_highday_price = json_decode($row->group_highday_price,true);
        } else {
            $row->group_highday_price = [];
        }

        if ($row->group_stay_discount){
            $row->group_stay_discount = json_decode($row->group_stay_discount,true);
        } else {
            $row->group_stay_discount = [];
        }

        $this->view->assign("row", $row);

        //房屋类型
        $house_type_model = new HouseType();
        $type_list = $house_type_model->getTypeList();
        $house_type_list = [];
        foreach ($type_list as $k=>$v){
            $list = HouseType::where(['type'=>$k])->where('type_id','in',$row->house_type_id)
                ->order('weigh desc')->field('type_id,name')->select();

            if (count($list)>0){
                $house_type_list[] = [
                    'title'=>$v,
                    'data'=>$list
                ];
            }
        }
        //dump($house_type_list);die;
        $this->view->assign("house_type_list", $house_type_list);

        //床铺信息
        $bed_list = $this->HouseBedInfoModel::where(['house_id'=>$ids])->select();
        $this->view->assign("bed_list", $bed_list);

        //共享模式
        $mode = explode(',',$row->mode);
        $this->view->assign("mode_arr", $mode);

        $bed_index = $this->HouseBedInfoModel::where('index','in',$row->bed_index)->where('house_id',$ids)->column('bed_name');
        $this->view->assign("bed_index", implode(',',$bed_index));

        //房屋设施
        //$house_facilities_model = new HouseFacilities();
        //$type_list = $house_facilities_model->getTypeList();
        $type_list = HouseFacilitiesCategory::order('weigh desc')->select();
        $facilities_list = [];
        foreach ($type_list as $k=>$v){
            $list = HouseFacilities::where(['category_id'=>$v['id']])->order('weigh desc')->field('name')->select();
            if (count($list)>0){
                $facilities_list[] = [
                    'title'=>$v['name'],
                    'data'=>$list
                ];
            }
        }
        $this->view->assign("facilities_list", $facilities_list);

        //图片
        $media_list = [];
        $media_category = HouseMediaCategory::where(['type'=>0])->order('weigh desc')->field('id,name')->select();
        foreach ($media_category as $k=>$v){
            $list = HouseMedia::where(['house_id'=>$ids,'category_id'=>$v['id']])->field('media')->select();

            if (count($list)>0){
                $media_list[] = [
                    'title'=>$v['name'],
                    'data'=>$list
                ];
            }
        }
        $this->view->assign("media_list", $media_list);

        //配套服务
        $complement = $this->HouseComplementPriceModel::where(['house_id'=>$ids])
            ->field('id,house_id,name,unit,price')
            ->select();
        $this->view->assign("complement", $complement);
        return $this->view->fetch();
    }

    //园区详情
    public function parkdetail($ids = null)
    {
        $row = $this->HouseParkModel::get(['house_id'=>$ids]);

        if (!$row)$this->error('园区不存在');
        $park_id = $row->park_id;
        $this->view->assign("row", $row);

        //图片
        $media_list = [];
        $media_category = HouseMediaCategory::where(['type'=>1])->order('weigh desc')->field('id,name')->select();

        foreach ($media_category as $k=>$v){
            $list = HouseParkMedia::where(['park_id'=>$park_id,'category_id'=>$v['id']])->select();

            if (count($list)){
                $media_list[] = [
                    'title'=>$v['name'],
                    'data'=>$list
                ];
            }
        }
        $this->view->assign("media_list", $media_list);

        //园区配套
        $house_park_complement_model = new HouseParkComplement();
        $type_list = $house_park_complement_model->getTypeList();
        $park_complement_list = [];
        foreach ($type_list as $k=>$v){
            $list = HouseParkComplement::where(['type'=>$k])
                ->where('id','in',$row->complement_id)
                ->order('weigh desc')->field('name')->select();
            if (count($list)>0){
                $park_complement_list[] = [
                    'title'=>$v,
                    'data'=>$list
                ];
            }
        }
        $this->view->assign("park_complement_list", $park_complement_list);
        //物业管理
        $manage_category_list = HouseManageCategory::order('weigh desc')->field('id,name')->select();
        foreach ($manage_category_list as $k=>$v){
            $manage_category_list[$k]['child'] = HouseManage::where('id','in',$row->manage_id)
                ->order('weigh desc')->field('name')->select();
        }
        $this->view->assign("manage_list", $manage_category_list);
        return $this->view->fetch();
    }

    public function pass($ids = null)
    {
        $row = $this->model->get($ids);

        $row->status=1;

        if ($row->save()){
            \app\admin\model\house\HousePark::where(['house_id'=>$ids])->update(['status'=>1]);

            $change_house_level = $this->model->where(['status'=>1,'user_id'=>$row->user_id])
                ->where('FIND_IN_SET(:mode,mode)',['mode' => 3])
                ->order('house_level desc')
                ->field('house_level')->find();

            $user = User::get($row->user_id);

            if ($change_house_level){
                $user->level_type = 3;
            } else {
                if ($user->level_type==1){
                    $user->level_type = 2;
                }
                if ($user->level_type==4){
                    $user->level_type = 5;
                }
            }

            if ($row->house_level>$user->level){
                $user->level = $row->house_level;
            }

            $user->save();
            $this->success('审核成功');
        } else {
            $this->error('审核失败');
        }
    }

    public function refuse($ids = null)
    {
        $row = $this->model->get($ids);
        $row->status = 2;

        if ($row->save()){

            $this->success('拒绝成功');
        } else {
            $this->error('拒绝失败');
        }
    }

    public function recommend($ids = null)
    {
        $row = $this->model->get($ids);

        $row->is_recommend=1;

        if ($row->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    public function cancelrecommend($ids = null)
    {
        $row = $this->model->get($ids);

        $row->is_recommend=0;

        if ($row->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    public function settop($ids = null)
    {
        $row = $this->model->get($ids);

        $row->is_top=1;

        if ($row->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    public function canceltop($ids = null)
    {
        $row = $this->model->get($ids);

        $row->is_top=0;

        if ($row->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }
}
